<template>
  <el-dialog id="rename" title="重命名" :visible.sync="renameVisible">
    <div>
      <div class="rename-header">
        <div class="content">
          <span>原文件名</span>
        </div>
        <div class="content">
          <span>修改</span>
        </div>
      </div>
      <div class="rename-cont">
        <div class="content">
          <span>{{olderName}}</span>
        </div>
        <div class="rename-box content">
          <span>
            <el-input
              v-model="renameForm.prename"
              autocomplete="off"
              clearable
              @keyup.enter.native="renameSubmit"
              autofocus="true"
              v-if="renameForm.fileType == 0"
              @blur="checkname"
            >
              <template slot="append">.{{renameForm.suffix}}</template>
            </el-input>
            <el-input
              v-model="renameForm.name"
              autocomplete="off"
              clearable
              @keyup.enter.native="renameSubmit"
              autofocus="true"
              v-else
              @blur="checkname"
            ></el-input>
          </span>
        </div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="renameVisible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="renameSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "RenameFile",
  data() {
    return {
      renameVisible: false,
      olderName: "",
      renameForm: {
        name: "",
        prename: "",
        suffix: "",
        fileType: 0
      },
      infoTemp: ""
    };
  },
  methods: {
    renameHandle(info) {
      console.log(info);
      this.infoTemp = "";
      this.renameVisible = true;
      var pos = info.fileName.lastIndexOf("."); //查找最后一个\的位置
      this.renameForm.prename = info.fileName.substring(0, pos);
      this.renameForm.dirId = info.dirId;
      this.renameForm.suffix = info.fileName.substring(pos + 1);
      this.renameForm.name = info.fileName;
      this.renameForm.fileType = info.fileType;
      this.olderName = info.fileName;
      this.infoTemp = info;
    },
    checkname() {
      let name = "";
      var re = / [^ ]+/;
      var ree = /(^\s+)|(\s+$)/g;
      this.renameForm.fileType == 0
        ? (name = this.renameForm.prename)
        : (name = this.renameForm.name);
      console.log(name);
      var reg = new RegExp('^[^\\\\\\/:.*?\\"<>| ]+$');
      if (!(reg.test(name)) || re.test(name) || ree.test(name)) {
        this.$message.error("文件名不符合规则");
        return false;
      } else if (name.length > 20) {
        this.$message.error("文件名长度超出最大限制");
        return false;
      }
      return true;
    },
    renameSubmit() {
      if (!this.checkname()) {
        return;
      }
      let para = {
        id: this.infoTemp.id,
        name:
          this.renameForm.fileType == 0
            ? this.renameForm.prename + "." + this.renameForm.suffix
            : this.renameForm.name
      };
      this.$confirm("确认重命名该文件吗？", "提示", {
        beforeClose(action,instance,done){
          console.log(action,instance,done)
          if(action == 'confirm'){
            console.log(instance.$refs['confirm'].$el);
            instance.$refs['confirm'].$el.onclick = function (e) {
              console.log(e);
              if(e.detail != 0){
                done();
              }
            }
          }else{
            done();
          }
        }
      }).then(() => {
        this.$http.post("/file/reName", para).then(res => {
          console.log(res);
          if (res.status === 200) {
            this.$emit("reloadData", this.renameForm.dirId);
            this.renameVisible = false;
            this.$message({
              type: "success",
              message: "重命名成功"
            });
          } else if (res.status === 500) {
            this.$message({
              type: "error",
              message: "文件名重复"
            });
          } else {
            this.$message({
              type: "error",
              message: "重命名失败"
            });
          }
        });
      });
    }
  }
};
</script>

<style scoped lang="scss">
#rename {
  .el-dialog__body {
    padding: 0;
  }
  .el-dialog__header {
    padding: 10px;
    background: #374f6b;
    .el-dialog__title {
      color: #fff;
      font-size: 14px;
    }
    .el-dialog__headerbtn {
      top: 10px;
    }
  }
  .content {
    width: 50%;
  }
  .rename-header {
    line-height: 30px;
    display: flex;
  }
  .rename-cont {
    display: flex;
    padding-top: 15px;
  }
  .el-input {
    width: 100%;
    input {
      height: 30px;
      font-size: 12px;
    }
  }
}
</style>
